package jmu.edu.yjc.controller;

import jmu.edu.yjc.Utils.BeanCopyUtil;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.Elderly;
import jmu.edu.yjc.domain.entity.Healthy;
import jmu.edu.yjc.domain.vo.healthy.HealthyVo;
import jmu.edu.yjc.service.ElderlyService;
import jmu.edu.yjc.service.HealthyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yjc
 * @date 2023/4/5 23:27
 */
@RestController
@RequestMapping("/personal/healthy")
public class HealthyController {
    @Resource
    private HealthyService healthyService;
    @Resource
    private ElderlyService elderlyService;
    @GetMapping("/list")
    public ResponseResult listHealthy(Integer pageSize, Integer pageNum ,
                                      String name, String startTime, String endTime) {
        return healthyService.listHealthy(pageSize,pageNum,name,startTime,endTime);
    }
    @PostMapping("/add")
    public ResponseResult addHealthy(@RequestBody Healthy healthy) {
        return ResponseResult.okResult(healthyService.save(healthy));
    }
    @PostMapping("/update")
    public ResponseResult updateHealthy(@RequestBody Healthy healthy) {
        return ResponseResult.okResult(healthyService.updateById(healthy));
    }
    @GetMapping("/{id}")
    public ResponseResult getHealthyById(@PathVariable("id") Long id) {
        Healthy healthy = healthyService.getById(id);
        HealthyVo healthyVo = BeanCopyUtil.copyBeans(healthy, HealthyVo.class);
        healthyVo.setElderly(elderlyService.getByIdentity(healthyVo.getIdentityId()));
        return ResponseResult.okResult(healthyVo);
    }
    @DeleteMapping("/{id}")
    public ResponseResult deleteHealthy(@PathVariable("id") Long id) {
        return ResponseResult.okResult(healthyService.removeById(id));
    }
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        healthyService.export(response);
    }
    @PreAuthorize(("@ps.hasPermission('personal:healthy:upload')"))
    @PostMapping("/upload")
    public ResponseResult uploadExcel(@RequestPart("file") MultipartFile file){
        return healthyService.uploadExcel(file);
    }
    @GetMapping("/getElderly")
    public ResponseResult getElderly(String name) {
        return elderlyService.getElderly(name);
    }
}
